package cn.edu.seig.textbook.service.impl;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author lenovo
 */
@Service
public class DumpSQLService {
    public void dataBaseDump(String host, String port, String username, String password, String dbName, String currentDate){
        String path="F:/database/";
        File file=new File(path);
        if(!file.exists()){
            file.mkdirs();
        }
        File file1=new File(path+dbName+currentDate+".sql");
        String cmd="cmd /c mysqldump -h"+host+" -p"+port+" -u"+username+" -p"+password+" "+dbName+">"+file1.getAbsolutePath();

        try {
            Process process=Runtime.getRuntime().exec(cmd);
            System.out.println("数据库备份成功，备份时间："+currentDate);
        }catch (Exception e){
            System.out.println("数据库备份失败！");
            throw new RuntimeException(e);
        }

    }
    @Scheduled(cron = "0 0 0 * * *")
    public void dump(){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd_HHmmss");
        String currentDate=sdf.format(new Date());
        dataBaseDump("localhost","3306","javase","javase","textbook",currentDate);
    }
}
